/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.exceptions.IllegalOrphanException;
import dao.exceptions.NonexistentEntityException;
import ecfdb.Legumes;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ecfdb.LegumesDepot;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author QEMLAB
 */
public class LegumesJpaController implements Serializable {

    public LegumesJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Legumes legumes) {
        if (legumes.getLegumesDepotList() == null) {
            legumes.setLegumesDepotList(new ArrayList<LegumesDepot>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<LegumesDepot> attachedLegumesDepotList = new ArrayList<LegumesDepot>();
            for (LegumesDepot legumesDepotListLegumesDepotToAttach : legumes.getLegumesDepotList()) {
                legumesDepotListLegumesDepotToAttach = em.getReference(legumesDepotListLegumesDepotToAttach.getClass(), legumesDepotListLegumesDepotToAttach.getIdlegumesDepot());
                attachedLegumesDepotList.add(legumesDepotListLegumesDepotToAttach);
            }
            legumes.setLegumesDepotList(attachedLegumesDepotList);
            em.persist(legumes);
            for (LegumesDepot legumesDepotListLegumesDepot : legumes.getLegumesDepotList()) {
                Legumes oldLegumesOfLegumesDepotListLegumesDepot = legumesDepotListLegumesDepot.getLegumes();
                legumesDepotListLegumesDepot.setLegumes(legumes);
                legumesDepotListLegumesDepot = em.merge(legumesDepotListLegumesDepot);
                if (oldLegumesOfLegumesDepotListLegumesDepot != null) {
                    oldLegumesOfLegumesDepotListLegumesDepot.getLegumesDepotList().remove(legumesDepotListLegumesDepot);
                    oldLegumesOfLegumesDepotListLegumesDepot = em.merge(oldLegumesOfLegumesDepotListLegumesDepot);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Legumes legumes) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Legumes persistentLegumes = em.find(Legumes.class, legumes.getIdlegumes());
            List<LegumesDepot> legumesDepotListOld = persistentLegumes.getLegumesDepotList();
            List<LegumesDepot> legumesDepotListNew = legumes.getLegumesDepotList();
            List<String> illegalOrphanMessages = null;
            for (LegumesDepot legumesDepotListOldLegumesDepot : legumesDepotListOld) {
                if (!legumesDepotListNew.contains(legumesDepotListOldLegumesDepot)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain LegumesDepot " + legumesDepotListOldLegumesDepot + " since its legumes field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<LegumesDepot> attachedLegumesDepotListNew = new ArrayList<LegumesDepot>();
            for (LegumesDepot legumesDepotListNewLegumesDepotToAttach : legumesDepotListNew) {
                legumesDepotListNewLegumesDepotToAttach = em.getReference(legumesDepotListNewLegumesDepotToAttach.getClass(), legumesDepotListNewLegumesDepotToAttach.getIdlegumesDepot());
                attachedLegumesDepotListNew.add(legumesDepotListNewLegumesDepotToAttach);
            }
            legumesDepotListNew = attachedLegumesDepotListNew;
            legumes.setLegumesDepotList(legumesDepotListNew);
            legumes = em.merge(legumes);
            for (LegumesDepot legumesDepotListNewLegumesDepot : legumesDepotListNew) {
                if (!legumesDepotListOld.contains(legumesDepotListNewLegumesDepot)) {
                    Legumes oldLegumesOfLegumesDepotListNewLegumesDepot = legumesDepotListNewLegumesDepot.getLegumes();
                    legumesDepotListNewLegumesDepot.setLegumes(legumes);
                    legumesDepotListNewLegumesDepot = em.merge(legumesDepotListNewLegumesDepot);
                    if (oldLegumesOfLegumesDepotListNewLegumesDepot != null && !oldLegumesOfLegumesDepotListNewLegumesDepot.equals(legumes)) {
                        oldLegumesOfLegumesDepotListNewLegumesDepot.getLegumesDepotList().remove(legumesDepotListNewLegumesDepot);
                        oldLegumesOfLegumesDepotListNewLegumesDepot = em.merge(oldLegumesOfLegumesDepotListNewLegumesDepot);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = legumes.getIdlegumes();
                if (findLegumes(id) == null) {
                    throw new NonexistentEntityException("The legumes with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Legumes legumes;
            try {
                legumes = em.getReference(Legumes.class, id);
                legumes.getIdlegumes();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The legumes with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<LegumesDepot> legumesDepotListOrphanCheck = legumes.getLegumesDepotList();
            for (LegumesDepot legumesDepotListOrphanCheckLegumesDepot : legumesDepotListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Legumes (" + legumes + ") cannot be destroyed since the LegumesDepot " + legumesDepotListOrphanCheckLegumesDepot + " in its legumesDepotList field has a non-nullable legumes field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(legumes);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Legumes> findLegumesEntities() {
        return findLegumesEntities(true, -1, -1);
    }

    public List<Legumes> findLegumesEntities(int maxResults, int firstResult) {
        return findLegumesEntities(false, maxResults, firstResult);
    }

    private List<Legumes> findLegumesEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Legumes.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Legumes findLegumes(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Legumes.class, id);
        } finally {
            em.close();
        }
    }

    public int getLegumesCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Legumes> rt = cq.from(Legumes.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
